home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 2006 YourNameHere
- See the file LICENSE.txt for licensing information.
- */
- var WisestampCompose =
- {
- _prefs: null,
- app: null,
- currentType: "",
- open: false,
- sessionID: null,
- get prefs()
- {
- if (!this._prefs)
- {
- this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
- getService(Components.interfaces.nsIPrefService);
- this._prefs = this._prefs.getBranch("extensions.wisestamp.");
- }
- return this._prefs;
- },
-
- updateEditorHtml: function(editor,html)
- {
- editor.rootElement.innerHTML = html;
- },
-
- insertSignature: function(type)
- {
- try
- {
- var editor = GetCurrentEditor();
- if (editor instanceof Components.interfaces.nsIHTMLEditor)
- {
- var body = editor.document.documentElement;
- var html = body.innerHTML;
-
- var prefix = WisestampSignatureFactory.PREFIX.replace("SIG_","SIG_"+this.sessionID+"_");
- var suffix = WisestampSignatureFactory.SUFFIX.replace("SIG_","SIG_"+this.sessionID+"_");
- var sig = prefix + WisestampSignatureFactory.createSignature(null, type) + suffix;
- var newBody = null;
-
- var pattern = new RegExp(prefix.replace("!", "\!") + "(.|\n)*" + suffix.replace("!", "\!"));
- if (html.match(pattern))
- newBody = html.replace(pattern, sig);
- else
- newBody = html.replace(/(\<body.*?\>)/, "$1"+"<br/><br/>" + sig);
-
- this.updateEditorHtml(editor,newBody);
- }
-
- } catch(e)
- {
- WiseStampUtils.log("[thunderbirdCompose.js::insertSignature] exception caught: " + e);
- }
- },
-
- onUseSignature: function (event, type, popup)
- {
- this.insertSignature(type);
- },
-
- onCompose: function (event, type, popup)
- {
- WiseStampUtils.log("[thunderbirdCompose.js::onCompose] >>>>> ");
-
- var autoInsertOnCompose = WiseStampPrefs.getBoolPref("autoinsert_compose",true,"autoinsert");
- var autoInsertOnThread = WiseStampPrefs.getBoolPref("autoinsert_thread",true,"autoinsert");
-
- var newMessage = (gMsgCompose.compFields.subject == "");
- if ((newMessage && !autoInsertOnCompose) || (!newMessage && !autoInsertOnThread))
- return;
-
- this.insertSignature(type);
- },
-
- onLoad: function ()
- {
- WiseStampUtils.log("[thunderbirdCompose.js::onLoad] >>>>> ");
-
- this.sessionID = WiseStampUtils.getRandomInt(10000,99999);
-
- this.currentType = WisestampCompose.prefs.getCharPref("type");
- WisestampOverlay.app = WisestampGetAppObject();
- if (!this.prefs.getBoolPref("enabled"))
- document.getElementById("wisestampButton").setAttribute("disabled", "true");
-
- var hide = this.prefs.getBoolPref("hidebuttons");
- var bttn = document.getElementById("wisestampButton");
- bttn.setAttribute("hidden", hide);
- this.open = true;
-
- //WisestampCompose.onUseSignature(null,this.currentType);
- setTimeout(function(){WisestampCompose.onCompose(null,this.currentType);},500,null);
- },
-
- onFocus: function ()
- {
- if (!this.open)
- {
- this.onLoad();
- }
- },
-
- onClose: function ()
- {
- this.open = false;
- },
-
- unLoad: function ()
- {
- //if (gMsgCompose)
- // gMsgCompose.UnregisterStateListener(WisestampStateListener);
- }
- }
-
- function WisestampGetAppObject()
- {
- var app = new WisestampThunderbird();
- app.onLoad = function (){};
- return app;
- }
-
- window.addEventListener("compose-window-reopen",function(e){WisestampCompose.onLoad()},true);
- window.addEventListener("load",function(e){WisestampCompose.onLoad()},true);
- window.addEventListener("close",function(){WisestampCompose.onClose()},true);
- window.addEventListener("unload",function(){WisestampCompose.unLoad()},false);
- document.documentElement.addEventListener("focus",function(){WisestampCompose.onFocus()},true);
-